package zack.project.domain.credit.service.adjust;

import zack.project.domain.credit.adapter.repository.ICreditRepository;
import zack.project.domain.credit.event.CreditAdjustSuccessMessageEvent;
import zack.project.domain.credit.model.aggregate.TradeAggregate;
import zack.project.domain.credit.model.entity.CreditAccountEntity;
import zack.project.domain.credit.model.entity.CreditOrderEntity;
import zack.project.domain.credit.model.entity.TaskEntity;
import zack.project.domain.credit.model.entity.TradeEntity;
import zack.project.domain.credit.service.ICreditAdjustService;
import zack.project.types.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class CreditAdjustService implements ICreditAdjustService {

    @Resource
    ICreditRepository creditRepository;
    @Resource
    CreditAdjustSuccessMessageEvent creditAdjustSuccessMessageEvent;

    /**
     * 根据交易实体对象
     * @param tradeEntity
     * @return
     */
    @Override
    public String createOrder(TradeEntity tradeEntity) {
        //构建用户积分实体，设置积分的变更量
        CreditAccountEntity creditAccountEntity = TradeAggregate
                .buildCreditAccountEntity(tradeEntity.getUserId(), tradeEntity.getAmount());
        //构建积分订单实体
        //id user_id    order_id        trade_name  trade_type  trade_amount    out_business_no
        //1	xiaofuge25	479481818661	行为返利	    forward	    10.00	        xiaofuge25_integral_20240622	2024-06-22 10:07:41	2024-06-22 10:07:41
        //2	xiaofuge25	981030218405	兑换抽奖	    reverse	    -20.00	        404066146511	                2024-06-22 10:11:50	2024-06-22 10:11:50
        CreditOrderEntity creditOrderEntity = TradeAggregate.buildCreditOrderEntity(
                tradeEntity.getUserId(),
                tradeEntity.getAmount(),
                tradeEntity.getTradeNameVO(),
                tradeEntity.getTradeTypeVO(),
                tradeEntity.getOutBusinessNo()
        );
        //构建积分调整消息内容,
        // 要包含积分调额订单的订单号(user_credit_order)，外部订单号(如果是积分兑换此处则是未支付的活动单号)
        CreditAdjustSuccessMessageEvent.CreditAdjustSuccessMessage creditAdjustSuccessMessage =
                new CreditAdjustSuccessMessageEvent.CreditAdjustSuccessMessage();
        creditAdjustSuccessMessage.setOrderId(creditOrderEntity.getOrderId());
        creditAdjustSuccessMessage.setAmount(tradeEntity.getAmount());
        creditAdjustSuccessMessage.setOutBusinessNo(tradeEntity.getOutBusinessNo());
        creditAdjustSuccessMessage.setUserId(tradeEntity.getUserId());
        //构建积分调整消息
        BaseEvent.EventMessage<CreditAdjustSuccessMessageEvent.CreditAdjustSuccessMessage> creditAdjustSuccessMessageEventMessage =
                creditAdjustSuccessMessageEvent.buildEventMessage(creditAdjustSuccessMessage);
        //构建积分调整补偿任务
        TaskEntity taskEntity =
                TradeAggregate.buildTaskEntity(tradeEntity.getUserId(),
                        creditAdjustSuccessMessageEvent.topic(),
                        creditAdjustSuccessMessageEventMessage.getId(),
                        creditAdjustSuccessMessageEventMessage);
        //构建交易聚合实体:积分账户(要修改的amount)，积分订单，补偿任务
        TradeAggregate tradeAggregate = new TradeAggregate();
        tradeAggregate.setUserId(tradeEntity.getUserId());
        tradeAggregate.setOrderEntity(creditOrderEntity);
        tradeAggregate.setAccountEntity(creditAccountEntity);
        tradeAggregate.setTaskEntity(taskEntity);


        creditRepository.saveUserCreditTradeOrder(tradeAggregate);
        log.info("变更账户积分额度完成 userId:{} orderId:{}", tradeEntity.getUserId(), creditOrderEntity.getOrderId());

        return creditOrderEntity.getOrderId();
    }

    @Override
    public CreditAccountEntity queryUserCreditAccount(String userId) {
        return creditRepository.queryUserCreditAccount(userId);
    }
}
